
package codeGenerate.custom.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import codeGenerate.common.utils.SystemUtils;
import codeGenerate.custom.converter.impl.DefaultCustomConverter;
import codeGenerate.custom.generater.CustomGenerater;
import codeGenerate.custom.tempalte.TemPathCustom;
import codeGenerate.custom.vo.CustomConfigVo;

/**
 * 自定义工具类
 * @author: dw-fu1
 */
public class CustomGenUtils {

	/**
	 * 生成代码
	 * @author fudewei
	 * @param configPath 配置文件路径
	 */
	public static void genCode(String configPath) throws Exception {
		File file = new File(configPath);
		if (!file.exists()) {
			System.out.println("文件不存在：" + configPath);
		}
		FileInputStream is = new FileInputStream(file);
		genCode(is);
	}

	/**
	 * 生成代码
	 * @author fudewei
	 * @param is  输入流
	 */
	public static void genCode(InputStream is) throws Exception {
		List<CustomConfigVo> configList = readConfig(is);
		for (CustomConfigVo cf : configList) {
			CustomGenerater gen = new CustomGenerater();
			gen.setConfigVo(cf);
			gen.generateCode();
		}
	}

	@SuppressWarnings("unchecked")
	public static List<CustomConfigVo> readConfig(InputStream is) throws ParserConfigurationException, SAXException, IOException, DocumentException {
		try {
			List<CustomConfigVo> configVoList = new ArrayList<>();
			//创建解析器
			SAXReader reader = new SAXReader();
			//得到document
			Document document = reader.read(is);
			Element root = document.getRootElement();
			String createUser = root.elementText("createUser");
			String copyright = root.elementText("copyright");
			String template = root.elementText("template");
			String genPath = root.elementText("genPath");
			String templateDirectory = root.elementText("templateDirectory");
			if (StringUtils.isBlank(template)) {
				template = TemPathCustom.class.getName();
			}
			if (StringUtils.isBlank(templateDirectory)) {
				templateDirectory = SystemUtils.defalutTemplateDirectory();
			}
			String converter = StringUtils.defaultIfBlank(root.elementText("converter"), DefaultCustomConverter.class.getName());
			Element beansEle = root.element("beans");

			List<Element> beanEles = beansEle.elements("bean");
			for (Element beanEle : beanEles) {
				CustomConfigVo cv = new CustomConfigVo();
				cv.setCreateUser(createUser);
				cv.setCopyright(copyright);
				cv.setTemplate(template);
				cv.setGenPath(genPath);
				cv.setBeanDescription(beanEle.attributeValue("desc"));
				cv.setBeanName(StringUtils.defaultIfBlank(beanEle.attributeValue("beanName"), SystemUtils.getBeanNameByPath(beanEle.attributeValue("path"))));
				//避免beanName首字母小写
				cv.setBeanName(StringUtils.capitalize(cv.getBeanName()));
				cv.setPack(beanEle.attributeValue("path"));
				cv.setResponseType(beanEle.attributeValue("responseType"));
				cv.setTemplateDirectory(templateDirectory);
				cv.setCreateTime(new Date());
				cv.setRequest(beanEle.elementText("request"));
				cv.setResponse(beanEle.elementText("response"));
				Map<String, String> beanAttributeMap = new HashMap<>();
				List<Attribute> attributes = beanEle.attributes();
				for (Attribute attr : attributes) {
					beanAttributeMap.put(attr.getName(), attr.getValue());
				}
				cv.setBeanAttributeMap(beanAttributeMap);
				cv.setConverter(converter);
				configVoList.add(cv);
			}
			return configVoList;
		} finally {
			is.close();
		}

	}

	public static void main(String[] args) {
		System.out.println(StringUtils.defaultIfBlank("xx", "defaultStr"));
	}
}
